import pygame

from Map import Map,SCREEN_HEIGHT,SCREEN_WIDTH
from base import BaseItem
from Animation import Explode
from Music import Music


class Bullet(BaseItem):

    def __init__(self, rect,direction,speed = 6):
        super(Bullet, self).__init__()
        # 加载子弹图片
        self.image = pygame.image.load('img/enemymissile.gif')
        # 坦克的朝向决定子弹的发射的方向
        self.direction = direction
        # 获取子弹显示区域
        self.rect = self.image.get_rect()
        # 子弹的left和top与方向有关
        if direction == 'U':
            self.rect.left = rect.left + rect.width/2 - self.rect.width/2
            self.rect.top = rect.top - self.rect.height
        elif direction == 'D':
            self.rect.left = rect.left + rect.width/2 - self.rect.width/2
            self.rect.top = rect.top + rect.height
        elif direction == 'L':
            self.rect.left = rect.left - self.rect.width
            self.rect.top = rect.top + rect.width/2 - self.rect.width/2
        elif direction == 'R':
            self.rect.left = rect.left + rect.height
            self.rect.top = rect.top + rect.width/2 - self.rect.width/2
        # 设置子弹的速度
        self.speed = speed
        # 子弹是否到达边界标志
        self.live = True

    def hitWall(self):
        # 循环遍历墙壁列表
        for wall in Map.wallList:
            if self.hit(wall):
                # 子弹消失
                self.live = False
                # 墙壁的生命值减少
                wall.hp -= 1
                if wall.hp <= 0:
                    # 墙壁消失
                    wall.live = False

    def move(self):
        if self.direction == 'U':
            if self.rect.top > 0:
                self.rect.top -= self.speed
            else:
                # 修改子弹状态
                self.live = False
        elif self.direction == 'D':
            if self.rect.top+self.rect.height < SCREEN_HEIGHT:
                self.rect.top += self.speed
            else:
                # 修改子弹状态
                self.live = False
        elif self.direction == 'L':
            if self.rect.left > 0:
                self.rect.left -= self.speed
            else:
                # 修改子弹状态
                self.live = False
        elif self.direction == 'R':
            if self.rect.left+self.rect.width < SCREEN_WIDTH:
                self.rect.left += self.speed
            else:
                # 修改子弹状态
                self.live = False

    def hitEnemyTank(self):
        """判断我方子弹是否射中敌方坦克"""
        hitNum = 0
        for enemyTank in Map.enemyTankList:
            # 循环遍历敌方坦克
            if self.hit(enemyTank):
                hitNum += 1
                enemyTank.live = False
                self.live = False
                # 创建爆炸对象
                explode = Explode(enemyTank)
                explodeMusic = Music('img/fire.wav')
                explodeMusic.play()
                # 将爆炸属性添加到爆炸列表中
                Map.explodeList.append(explode)
        return hitNum

    def hitMyTank(self):
        """敌方子弹与我方坦克碰撞"""
        if Map.myTank and Map.myTank.live:
            if self.hit(Map.myTank):
                # 产生爆炸对象
                explode = Explode(Map.myTank.rect)
                # 将爆炸对象添加到爆炸列表中
                Map.explodeList.append(explode)
                # 修改敌方子弹与我方坦克的状态
                self.live = False
                Map.myTank.live = False